package com.ruoyi.web.controller.fantasy;

import java.util.List;

import com.huanzhidadi.fantasy.bean.domain.FantasyCityMap;
import com.huanzhidadi.fantasy.bean.domain.FantasyTitleType;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.fantasy.service.IFantasyTitleTypeService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.huanzhidadi.fantasy.bean.domain.FantasyTitle;
import com.ruoyi.fantasy.service.IFantasyTitleService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 称号Controller
 * 
 * @author wanghui
 * @date 2020-04-11
 */
@Controller
@RequestMapping("/fantasy/fantasyTitle")
public class FantasyTitleController extends BaseController
{
    private String prefix = "fantasy/fantasyTitle";

    @Autowired
    private IFantasyTitleService fantasyTitleService;

    @Autowired
    private IFantasyTitleTypeService fantasyTitleTypeService;

    @RequiresPermissions("fantasy:fantasyTitle:view")
    @GetMapping()
    public String title(ModelMap mmap) {
        List<FantasyTitleType> list = fantasyTitleTypeService.selectFantasyTitleTypeList(null);
        mmap.addAttribute("titleTypes", list);
        return prefix + "/title";
    }

    /**
     * 查询称号列表
     */
    @RequiresPermissions("fantasy:fantasyTitle:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(FantasyTitle fantasyTitle)
    {
        startPage();
        List<FantasyTitle> list = fantasyTitleService.selectFantasyTitleList(fantasyTitle);
        fantasyTitleService.transforData(list);
        return getDataTable(list);
    }

    /**
     * 导出称号列表
     */
    @RequiresPermissions("fantasy:fantasyTitle:export")
    @Log(title = "称号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(FantasyTitle fantasyTitle)
    {
        List<FantasyTitle> list = fantasyTitleService.selectFantasyTitleList(fantasyTitle);
        ExcelUtil<FantasyTitle> util = new ExcelUtil<FantasyTitle>(FantasyTitle.class);
        return util.exportExcel(list, "title");
    }

    /**
     * 新增称号
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<FantasyTitleType> list = fantasyTitleTypeService.selectFantasyTitleTypeList(null);
        mmap.addAttribute("titleTypes", list);
        return prefix + "/add";
    }

    /**
     * 新增保存称号
     */
    @RequiresPermissions("fantasy:fantasyTitle:add")
    @Log(title = "称号", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(FantasyTitle fantasyTitle)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        fantasyTitle.setId(String.valueOf(IdWorker.nextId()));
        fantasyTitle.setCreateBy(user.getUserId().toString());
        return toAjax(fantasyTitleService.insertFantasyTitle(fantasyTitle));
    }

    /**
     * 修改称号
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<FantasyTitleType> list = fantasyTitleTypeService.selectFantasyTitleTypeList(null);
        mmap.addAttribute("titleTypes", list);
        FantasyTitle fantasyTitle = fantasyTitleService.selectFantasyTitleById(id);
        mmap.put("fantasyTitle", fantasyTitle);
        return prefix + "/edit";
    }

    /**
     * 修改保存称号
     */
    @RequiresPermissions("fantasy:fantasyTitle:edit")
    @Log(title = "称号", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(FantasyTitle fantasyTitle)
    {
        // 取身份信息
        SysUser user = ShiroUtils.getSysUser();
        fantasyTitle.setUpdateBy(user.getUserId().toString());
        return toAjax(fantasyTitleService.updateFantasyTitle(fantasyTitle));
    }

    /**
     * 删除称号
     */
    @RequiresPermissions("fantasy:fantasyTitle:remove")
    @Log(title = "称号", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(fantasyTitleService.deleteFantasyTitleByIds(ids));
    }

    /**
     * 称号管理-属性要求
     */
    @GetMapping("/propertyRequirement/{id}")
    public String propertyRequirement(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("titleId", id);
        return prefix + "/propertyRequirement";
    }

    /**
     * 称号管理-能力
     */
    @GetMapping("/titleAbility/{id}")
    public String titleAbility(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("titleId", id);
        return prefix + "/titleAbility";
    }

}
